﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using PanGu;

namespace RegexTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Segment.Init(System.IO.Path.GetFullPath(@".\PanGu.xml"));
        }

        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            if (txtRegex.Text != "" && txtOriginal.Text != "")
            {
                txtResult.Text = "";
                Regex regex = new Regex(txtRegex.Text);
                if (regex.IsMatch(txtOriginal.Text))
                {
                    string buffer = "";
                    buffer += regex.IsMatch(txtOriginal.Text).ToString();
                    foreach (Match match in regex.Matches(txtOriginal.Text))
                    {
                        buffer += match.Groups[1].ToString() + "\r\n";
                    }
                    txtResult.Text = buffer;
                }
                else
                {
                    txtResult.Text = "Not Match";
                }
            }

        }

        private void btnReplace_Click(object sender, RoutedEventArgs e)
        {
            if (txtRegex.Text != "" && txtOriginal.Text != "")
            {
                txtResult.Text = "";
                Regex regex = new Regex(txtRegex.Text);
                if (regex.IsMatch(txtOriginal.Text))
                {
                    txtResult.Text = regex.Replace(txtOriginal.Text, "");
                }
                else
                {
                    txtResult.Text = "Not Match";
                }
            }
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            string wordToTranslate = txtOriginal.Text;
            Segment segment = new Segment();
            //提取括号内词，如果不是英文或数字则删除
            if (Regex.IsMatch(wordToTranslate, @"\((.+)\)"))
            {
                ICollection<WordInfo> rs = segment.DoSegment(Regex.Match(wordToTranslate, @"\((.+)\)").Groups[1].ToString(), false);
                foreach (WordInfo word in rs)
                {
                    if (word.Pos.Equals(POS.POS_UNK) || word.Pos.Equals(POS.POS_A_NR))
                    {
                        if (!Regex.IsMatch(word.Word, @"^[0-9a-zA-Z-\. ]+$"))
                        {
                            wordToTranslate = new Regex(@"\(.*\)").Replace(wordToTranslate, "");
                        }
                    }
                }
            }
            if (Regex.IsMatch(wordToTranslate, @"（(.+)）"))
            {
                ICollection<WordInfo> rs = segment.DoSegment(Regex.Match(wordToTranslate, @"（(.+)）").Groups[1].ToString(), false);
                foreach (WordInfo word in rs)
                {
                    if (word.Pos.Equals(POS.POS_UNK) || word.Pos.Equals(POS.POS_A_NR))
                    {
                        if (!Regex.IsMatch(word.Word, @"^[0-9a-zA-Z-\. ]+$"))
                        {
                            wordToTranslate = new Regex(@"（.*\）").Replace(wordToTranslate, "");
                        }
                    }
                }
            }
            txtResult.Text = wordToTranslate;
        }



    }
}
